<?php
namespace app\common\exception;

use Firebase\JWT\ExpiredException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;

class Handler extends Handle {

    public function render($request, \Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
            exit;
        }
        
        // 请求异常
        if ($e instanceof HttpException && $request->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
            exit;
        }

        //登录错误
        if( $e instanceof AuthException ){
            return response($e->getMessage(),511);
            exit;
        }

        //token过期 重新登录
        if($e instanceof ExpiredException){
            return response("请重新登录",511);
            exit;
        }

        //登录错误
        if( $e instanceof WxappException ){
            return response($e->getMessage(),500);
            exit;
        }


        if($e instanceof BackAuthException){
            return response(['msg'=>$e->getMessage(),'code'=>0,'data'=>[]],200,[],'json');
            exit;
        }

        if($e instanceof \UnexpectedValueException){
            return response("请重新登录",511);
            exit;
        }

        if($e instanceof BackErrorException){
            return response(['msg'=>$e->getMessage(),'code'=>0,'data'=>[]],200,[],'json');
            //这里不推出 会继续抛出错误 其他异常也是一样 此处先暂时这样 后期再说
            exit;
        }



        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}